import com.formdev.flatlaf.*;
import project.*;
import project.Operation;
import project.WindowEventFrame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainForm {
    public static void main(String[] args) {

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    createGUI();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private static void createGUI() throws IOException {
        FlatLightLaf.install();
        initStyle();
        Operation.getConfig();
        Element.font = new Font(Element.fontFamily,Element.fontStyle,Element.fontSize);
        Frame frame = new Frame("EasyPad");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(1000,600);
        frame.setIconImage(new ImageIcon("src\\main\\resources\\images\\bitbug_favicon.png").getImage());
        Operation.setTheme(Element.themeStyle,frame);

        Element.styleJML.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Element.styleChooes = new StyleChooes();
                Element.styleChooes.showStyleDialog(frame);
            }
        });

        int windowWidth = frame.getWidth(); //获得窗口宽
        int windowHeight = frame.getHeight(); //获得窗口高
        Toolkit kit = Toolkit.getDefaultToolkit(); //定义工具包
        Dimension screenSize = kit.getScreenSize(); //获取屏幕的尺寸
        int screenWidth = screenSize.width; //获取屏幕的宽
        int screenHeight = screenSize.height; //获取屏幕的高
        frame.setLocation(screenWidth/2-windowWidth/2, screenHeight/2-windowHeight/2);//设置窗口居中显示

        frame.addWindowListener(new WindowEventFrame());
//        frame.setDefaultCloseOperation(0);
        Operation.setFormNoClose(frame);

                Element.fontJML.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 构造字体选择器，参数字体为预设值
                MQFontChooser fontChooser = new MQFontChooser(Element.font);
                // 打开一个字体选择器窗口，参数为父级所有者窗体。返回一个整型，代表设置字体时按下了确定或是取消，可参考MQFontChooser.APPROVE_OPTION和MQFontChooser.CANCEL_OPTION
                int returnValue = fontChooser.showFontDialog(frame);
                if (returnValue == 1) {
                    Element.font = new Font(Element.fontFamily,Element.fontSize,Element.fontSize);
                    System.out.println(Element.textList.size());
                    for (int i = 0; i < Element.textList.size(); i++) {
                        Element.textList.get(i).setFont(Element.font);
                    }
                }

            }
        });
    }
    /**
     * 设置全局控件样式
     * **/
    public static void initStyle() {
        UIManager.put("Menu.arc",100);

        //设置按钮样式
        UIManager.put( "Button.arc", 10 );

        //设置文本框样式
        UIManager.put( "TextComponent.arc", 1 );

        //设置选择框样式
//        UIManager.put( "TabbedPane.selectedBackground", Color.white );
        UIManager.put( "TabbedPane.showTabSeparators", true );
//        UIManager.put( "TabbedPane.tabSeparatorsFullHeight", true );
    }
}
